//
//  NotepadApp.swift
//  Notepad
//
//  Created by jerry on 2025/8/16.
//

import SwiftUI

@main
struct NotepadApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .frame(minWidth: 500, minHeight: 400)
                .onAppear {
                    // 设置应用程序菜单
                    setupMenus()
                }
        }
        .commands {
            // 添加自定义命令
            CommandGroup(replacing: .newItem) {
                Button("新建") {
                    NotificationCenter.default.post(name: Notification.Name("NewDocument"), object: nil)
                }
                .keyboardShortcut("n", modifiers: .command)
                
                Button("打开...") {
                    NotificationCenter.default.post(name: Notification.Name("OpenDocument"), object: nil)
                }
                .keyboardShortcut("o", modifiers: .command)
                
                Button("保存") {
                    NotificationCenter.default.post(name: Notification.Name("SaveDocument"), object: nil)
                }
                .keyboardShortcut("s", modifiers: .command)
                
                Button("另存为...") {
                    NotificationCenter.default.post(name: Notification.Name("SaveDocumentAs"), object: nil)
                }
                .keyboardShortcut("s", modifiers: [.command, .shift])
            }
            
            CommandGroup(replacing: .textEditing) {
                Button("查找...") {
                    NotificationCenter.default.post(name: Notification.Name("ToggleSearch"), object: nil)
                }
                .keyboardShortcut("f", modifiers: .command)
                
                Button("查找下一个") {
                    NotificationCenter.default.post(name: Notification.Name("FindNext"), object: nil)
                }
                .keyboardShortcut("g", modifiers: .command)
                
                Button("查找上一个") {
                    NotificationCenter.default.post(name: Notification.Name("FindPrevious"), object: nil)
                }
                .keyboardShortcut("g", modifiers: [.command, .shift])
            }
        }
    }
    
    // 设置应用程序菜单
    private func setupMenus() {
        // 这里可以添加更多自定义菜单设置
        // 注意：大部分菜单功能已通过 .commands 实现
    }
}

// 扩展通知名称
extension Notification.Name {
    static let newDocument = Notification.Name("NewDocument")
    static let openDocument = Notification.Name("OpenDocument")
    static let saveDocument = Notification.Name("SaveDocument")
    static let saveDocumentAs = Notification.Name("SaveDocumentAs")
    static let toggleSearch = Notification.Name("ToggleSearch")
    static let findNext = Notification.Name("FindNext")
    static let findPrevious = Notification.Name("FindPrevious")
}
